//常量
var oldSysUserName;
var oldSysUserPassword;
var oldSysUserStatus;
var oldIsSysUser;
var oldShopId;





//加载页面
$(document).ready(function () {
    var sysUserId = $("#sysUserId").val();
    if(null == sysUserId || "" == sysUserId || "undefined" == sysUserId){
        return;
    }
    
    $.ajax({
               url: webConfig.webUrl + '/getSysUserById',
               type: 'POST',
               dataType: 'json',
               data: {sysUserId: sysUserId}
           })
        .done(function (data) {
            if (data) {
                if (data.code == "10000" && data.result) {
                    $("#sysUserName").val(data.result.sysUser.userName);
                    $("#sysUserPassword").val(data.result.sysUser.password);
                    $("#sysUserStatus").val(data.result.sysUser.status);
                    $("#isSysUser").val(data.result.sysUser.isSysUser);
                    oldSysUserName = data.result.sysUser.userName;
                    oldSysUserPassword = data.result.sysUser.password;
                    oldSysUserStatus = data.result.sysUser.status;
                    oldIsSysUser = data.result.sysUser.isSysUser;

                    setSelectOptions('shop', data.result.shopList, 0, {
                        text: 'sname',
                        value: 'id'
                    });
                    $("#shop").val(data.result.sysUser.shopId);//门店ＩＤ


                    if (data.result.password != null) {
                        $("#sysUserPassword").attr("disabled", true);
                    }
                }
            }
        });
});

function setSelectOptions(eleId,data,defaultVal,keyObj) {
    if(document.getElementById(eleId)){
        var ele = document.getElementById(eleId);
        if(data){
            var options = {
                text:'text',
                value:'value'
            };
            if(keyObj){
                $.extend(options,keyObj);
            }
            for(var i=0; i < data.length; i++){
                ele.options.add(new Option(data[i][options.text],data[i][options.value]));
            }
        }else{
            console.error('数据为空');
        }

        if(defaultVal){
            for(var j=0; j < ele.options.length;j++){
                if(defaultVal == ele.options[j].value){
                    ele.options[j].selected = true;
                    break;
                }
            }
        }

    }else{
        console.error('元素不存在');
    }
}
//提交
$('#btnUpdate').click(function () {

    var sysUserInfo = {},
        id = $("#sysUserId").val(),
        url = webConfig.webUrl + '/addSysUser';


    if (null != id && "" != id && id != "undefined") {
        sysUserInfo.id = $("#sysUserId").val(); //用户id
        url = webConfig.webUrl + '/updateSysUser';
    }


    sysUserInfo.userName = $("#sysUserName").val();  //用户名称
    sysUserInfo.password = $("#sysUserPassword").val();  //用户密码
    sysUserInfo.status = $("#sysUserStatus").val();  //用户状态
    sysUserInfo.isSysUser = $("#isSysUser").val();  //是否系统用户
    sysUserInfo.shopId=$("#shop").val();//门店ＩＤ

    if (sysUserInfo.userName == null || sysUserInfo.userName == "") {
        layer.alert("请输入用户名", {icon: 2});
        return false;
    }

    if (sysUserInfo.password == null || sysUserInfo.password == "") {
        layer.alert("请输入密码", {icon: 2});
        return false;
    }

    if (oldSysUserName == sysUserInfo.userName && oldSysUserPassword == sysUserInfo.password
        && oldSysUserStatus == sysUserInfo.status && oldIsSysUser == sysUserInfo.isSysUser && oldShopId == sysUserInfo.shopId) {
        layer.alert("参数未变化", {icon: 2});
        return false;
    }


    $.ajax({
               url: url,
               type: 'POST',
               dataType: 'json',
               contentType: "application/json; charset=utf-8",
               data: JSON.stringify(sysUserInfo)
           })
        .done(function (data) {
            if (data) {
                if (data.code == "10000") {
                    layer.alert(data.message, {icon: 1}, function () {
                        parent.layer.closeAll('iframe');
                    });
                    return;
                } else {
                    layer.alert(data.message, {icon: 2});
                }
            }
        });
});


//取消
$('#btnCancel').click(function () {
    parent.layer.closeAll('iframe');
});
